package cn.sylinx.horm.example.section6;

import java.util.Map;

import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

import cn.sylinx.horm.core.SqlClient;
import cn.sylinx.horm.core.common.Page;
import cn.sylinx.horm.dialect.fs.FS;
import cn.sylinx.horm.example.entity.MyExample6;
import cn.sylinx.horm.resource.ClasspathSqlResource;
import cn.sylinx.horm.util.ParamMapBuilder;

@Profile("section6")
@Component
public class Section6 implements CommandLineRunner {

    private SqlClient sqlClient;
    
    private TestPageCommand testPageCommand;

    public Section6(SqlClient sqlClient, TestPageCommand testPageCommand) {
        this.sqlClient = sqlClient;
        this.testPageCommand = testPageCommand;
    }

    @Override
    public void run(String... args) throws Exception {
        testPage1();
        testPage2();
        testPage3();
    }

    void testPage1() {
        // MyExample6
        Page<MyExample6> page = sqlClient
                .queryPage(FS.of(MyExample6.class).where(MyExample6::getFieldName).like("name"), 1, 5);

        System.out.println("testPage1 totalRow -> " + page.getTotalRow());
        System.out.println("testPage1 totalPage -> " + page.getTotalPage());
        System.out.println("testPage1 pageSize -> " + page.getPageSize());
        System.out.println("testPage1 pageNumber -> " + page.getPageNumber());
        System.out.println("testPage1 pageData -> " + page.getList());
        System.out.println();
    }

    void testPage2() {
        // MyExample6
        Map<String, Object> params = new ParamMapBuilder().like("name", "name").build();
        Page<MyExample6> page = sqlClient.queryPage(ClasspathSqlResource.applySystemResource(SqlConsts.TEST_PAGE), 2, 5,
                params, MyExample6.class);

        System.out.println("testPage3 totalRow -> " + page.getTotalRow());
        System.out.println("testPage3 totalPage -> " + page.getTotalPage());
        System.out.println("testPage3 pageSize -> " + page.getPageSize());
        System.out.println("testPage3 pageNumber -> " + page.getPageNumber());
        System.out.println("testPage3 pageData -> " + page.getList());
        System.out.println();
        
    }
    
    void testPage3() {
        // MyExample6
        Map<String, Object> params = new ParamMapBuilder().like("name", "name").build();
        Page<MyExample6> page = testPageCommand.queryPage(params, 3, 5);

        System.out.println("testPage2 totalRow -> " + page.getTotalRow());
        System.out.println("testPage2 totalPage -> " + page.getTotalPage());
        System.out.println("testPage2 pageSize -> " + page.getPageSize());
        System.out.println("testPage2 pageNumber -> " + page.getPageNumber());
        System.out.println("testPage2 pageData -> " + page.getList());
        System.out.println();
        
    }
}
